{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "UEBilEjLj5wY"
   },
   "source": [
    "Deep Learning Models -- A collection of various deep learning architectures, models, and tips for TensorFlow and PyTorch in Jupyter Notebooks.\n",
    "- Author: Sebastian Raschka\n",
    "- GitHub Repository: https://github.com/rasbt/deeplearning-models"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {
     "autoexec": {
      "startup": false,
      "wait_interval": 0
     },
     "base_uri": "https://localhost:8080/",
     "height": 119
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 536,
     "status": "ok",
     "timestamp": 1524974472601,
     "user": {
      "displayName": "Sebastian Raschka",
      "photoUrl": "//lh6.googleusercontent.com/-cxK6yOSQ6uE/AAAAAAAAAAI/AAAAAAAAIfw/P9ar_CHsKOQ/s50-c-k-no/photo.jpg",
      "userId": "118404394130788869227"
     },
     "user_tz": 240
    },
    "id": "GOzuY8Yvj5wb",
    "outputId": "c19362ce-f87a-4cc2-84cc-8d7b4b9e6007"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sebastian Raschka \n",
      "\n",
      "CPython 3.7.3\n",
      "IPython 7.9.0\n",
      "\n",
      "torch 1.3.0\n"
     ]
    }
   ],
   "source": [
    "%load_ext watermark\n",
    "%watermark -a 'Sebastian Raschka' -v -p torch"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "rH4XmErYj5wm"
   },
   "source": [
    "# ResNet-34 QuickDraw Classifier"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Network Architecture"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The network in this notebook is an implementation of the ResNet-34 [1] architecture on the MNIST digits dataset (http://yann.lecun.com/exdb/mnist/) to train a handwritten digit classifier.  \n",
    "\n",
    "\n",
    "References\n",
    "    \n",
    "- [1] He, K., Zhang, X., Ren, S., & Sun, J. (2016). Deep residual learning for image recognition. In Proceedings of the IEEE conference on computer vision and pattern recognition (pp. 770-778). ([CVPR Link](https://www.cv-foundation.org/openaccess/content_cvpr_2016/html/He_Deep_Residual_Learning_CVPR_2016_paper.html))\n",
    "\n",
    "- [2] http://yann.lecun.com/exdb/mnist/\n",
    "\n",
    "![](../images/resnets/resnet34/resnet34-arch.png)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following figure illustrates residual blocks with skip connections such that the input passed via the shortcut matches the dimensions of the main path's output, which allows the network to learn identity functions.\n",
    "\n",
    "![](../images/resnets/resnet-ex-1-1.png)\n",
    "\n",
    "\n",
    "The ResNet-34 architecture actually uses residual blocks with skip connections such that the input passed via the shortcut matches is resized to dimensions of the main path's output. Such a residual block is illustrated below:\n",
    "\n",
    "![](../images/resnets/resnet-ex-1-2.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For a more detailed explanation see the other notebook, [resnet-ex-1.ipynb](resnet-ex-1.ipynb)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "MkoGLH_Tj5wn"
   },
   "source": [
    "## Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "colab": {
     "autoexec": {
      "startup": false,
      "wait_interval": 0
     }
    },
    "colab_type": "code",
    "id": "ORj09gnrj5wp"
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import time\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "from torch.utils.data import DataLoader\n",
    "from torch.utils.data import Dataset\n",
    "\n",
    "from torchvision import transforms\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "from PIL import Image\n",
    "\n",
    "\n",
    "if torch.cuda.is_available():\n",
    "    torch.backends.cudnn.deterministic = True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "I6hghKPxj5w0"
   },
   "source": [
    "## Model Settings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "colab": {
     "autoexec": {
      "startup": false,
      "wait_interval": 0
     },
     "base_uri": "https://localhost:8080/",
     "height": 85
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 23936,
     "status": "ok",
     "timestamp": 1524974497505,
     "user": {
      "displayName": "Sebastian Raschka",
      "photoUrl": "//lh6.googleusercontent.com/-cxK6yOSQ6uE/AAAAAAAAAAI/AAAAAAAAIfw/P9ar_CHsKOQ/s50-c-k-no/photo.jpg",
      "userId": "118404394130788869227"
     },
     "user_tz": 240
    },
    "id": "NnT0sZIwj5wu",
    "outputId": "55aed925-d17e-4c6a-8c71-0d9b3bde5637"
   },
   "outputs": [],
   "source": [
    "##########################\n",
    "### SETTINGS\n",
    "##########################\n",
    "\n",
    "# Hyperparameters\n",
    "RANDOM_SEED = 1\n",
    "LEARNING_RATE = 0.001\n",
    "BATCH_SIZE = 128\n",
    "NUM_EPOCHS = 10\n",
    "\n",
    "# Architecture\n",
    "NUM_FEATURES = 28*28\n",
    "NUM_CLASSES = 10\n",
    "\n",
    "# Other\n",
    "DEVICE = \"cuda:3\"\n",
    "GRAYSCALE = True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook is based on Google's Quickdraw dataset (https://quickdraw.withgoogle.com). In particular we will be working with an arbitrary subset of 10 categories in png format:\n",
    "\n",
    "    label_dict = {\n",
    "             \"lollipop\": 0,\n",
    "             \"binoculars\": 1,\n",
    "             \"mouse\": 2,\n",
    "             \"basket\": 3,\n",
    "             \"penguin\": 4,\n",
    "             \"washing machine\": 5,\n",
    "             \"canoe\": 6,\n",
    "             \"eyeglasses\": 7,\n",
    "             \"beach\": 8,\n",
    "             \"screwdriver\": 9,\n",
    "    }\n",
    "    \n",
    "(The class labels 0-9 can be ignored in this notebook). \n",
    "\n",
    "For more details on obtaining and preparing the dataset, please see the\n",
    "\n",
    "- [custom-data-loader-quickdraw.ipynb](custom-data-loader-quickdraw.ipynb)\n",
    "\n",
    "notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(28, 28)\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAQnUlEQVR4nO3da4xUdZoG8OcRGkFaAixt21HcRiG6BNxmKFB0NeAoCjHBMY4MH4D1skwMJDNmYryFDNEPELMzkyGuGmYhNGYWmeigfDDsEKPRMeHSEARadHWRnQFaugkXucj93Q99mDTa53+KOqfq1PT7/JJOd9dTp+pN6cOprn+dOjQziEjPd1neA4hIZajsIk6o7CJOqOwiTqjsIk70ruSdDRkyxBobGyt5lyKu7N69GwcOHGB3Waqyk7wPwG8B9ALwn2a2KHT9xsZGtLS0pLlLEQkoFAqxWclP40n2AvAfAKYAGAlgBsmRpd6eiJRXmr/ZxwP40sx2mdlpAG8AmJbNWCKStTRlvwbAX7v8vie67CIk55BsIdnS0dGR4u5EJI00Ze/uRYDvvffWzJaYWcHMCnV1dSnuTkTSSFP2PQCGdvn9WgD70o0jIuWSpuybAIwgOYxkHwA/AbAmm7FEJGslL72Z2VmS8wD8NzqX3paZWWtmk/Ugn3zySTDfsGFDMD9y5Egwv+yy+H+zR44ML5CElmoAQH969Ryp1tnN7F0A72Y0i4iUkd4uK+KEyi7ihMou4oTKLuKEyi7ihMou4kRFj2fvqZLWye++++5gfuzYsSzHyVTS5w8krdOH8qRtx44dG8wHD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mz6PJcH7vAXBV53Cr+NzvJXgD+B8A9APYA2ARghpl9WtFBYpDcDaBgZrm/AYPknQCOAVhhZqOiy14CcNDMFkX/UA4ys6erZLYFAI7lfRrv6GxFDV1PMw7gAQD/ihwfu8BcD6MCj1see/bxAL40s11mdhrAGwCm5TBH1TOzDwEc/M7F0wA0Rz83o/N/loqLma0qmFmbmW2Jfj4K4MJpxnN97AJzVUQeZb8GwF+7/L4H1XW+dwPwJ5KbSc7Je5hu1JtZG9D5Pw+Aq3Ke57sST+NdSd85zXjVPHalnP48rTzK3t2ppKpp/e92M/sBgCkA5kZPV6U4RZ3Gu1K6Oc14VSj19Odp5VH2PQCGdvn9WgD7cpijW2a2L/reDmA1qu9U1PsvnEE3+t6e8zx/U02n8e7uNOOogscuz9Of51H2TQBGkBxGsg+AnwBYk8Mc30Oyf/TCCUj2BzAZ1Xcq6jUAZkc/zwbwTo6zXKRaTuMdd5px5PzY5X76czOr+BeAqeh8Rf5/ATyfxwwxc10P4JPoqzXv2QCsROfTujPofEb0GIB/APAegC+i74OraLbXAWwHsA2dxWrIabZ/QeefhtsAbI2+pub92AXmqsjjprfLijihd9CJOKGyizihsos4obKLOKGyizihsos4obKLOPH/R9r9mQpsSDgAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "df = pd.read_csv('quickdraw_png_set1_train.csv', index_col=0)\n",
    "df.head()\n",
    "\n",
    "main_dir = 'quickdraw-png_set1/'\n",
    "\n",
    "img = Image.open(os.path.join(main_dir, df.index[99]))\n",
    "img = np.asarray(img, dtype=np.uint8)\n",
    "print(img.shape)\n",
    "plt.imshow(np.array(img), cmap='binary')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create a Custom Data Loader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "class QuickdrawDataset(Dataset):\n",
    "    \"\"\"Custom Dataset for loading Quickdraw images\"\"\"\n",
    "\n",
    "    def __init__(self, txt_path, img_dir, transform=None):\n",
    "    \n",
    "        df = pd.read_csv(txt_path, sep=\",\", index_col=0)\n",
    "        self.img_dir = img_dir\n",
    "        self.txt_path = txt_path\n",
    "        self.img_names = df.index.values\n",
    "        self.y = df['Label'].values\n",
    "        self.transform = transform\n",
    "\n",
    "    def __getitem__(self, index):\n",
    "        img = Image.open(os.path.join(self.img_dir,\n",
    "                                      self.img_names[index]))\n",
    "        \n",
    "        if self.transform is not None:\n",
    "            img = self.transform(img)\n",
    "        \n",
    "        label = self.y[index]\n",
    "        return img, label\n",
    "\n",
    "    def __len__(self):\n",
    "        return self.y.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Note that transforms.ToTensor()\n",
    "# already divides pixels by 255. internally\n",
    "\n",
    "\n",
    "BATCH_SIZE = 128\n",
    "\n",
    "custom_transform = transforms.Compose([#transforms.Lambda(lambda x: x/255.),\n",
    "                                       transforms.ToTensor()])\n",
    "\n",
    "train_dataset = QuickdrawDataset(txt_path='quickdraw_png_set1_train.csv',\n",
    "                                 img_dir='quickdraw-png_set1/',\n",
    "                                 transform=custom_transform)\n",
    "\n",
    "train_loader = DataLoader(dataset=train_dataset,\n",
    "                          batch_size=BATCH_SIZE,\n",
    "                          shuffle=True,\n",
    "                          num_workers=4) \n",
    "\n",
    "\n",
    "valid_dataset = QuickdrawDataset(txt_path='quickdraw_png_set1_valid.csv',\n",
    "                                img_dir='quickdraw-png_set1/',\n",
    "                                transform=custom_transform)\n",
    "\n",
    "valid_loader = DataLoader(dataset=valid_dataset,\n",
    "                          batch_size=BATCH_SIZE,\n",
    "                          shuffle=False,\n",
    "                          num_workers=4) \n",
    "\n",
    "\n",
    "\n",
    "test_dataset = QuickdrawDataset(txt_path='quickdraw_png_set1_train.csv',\n",
    "                                img_dir='quickdraw-png_set1/',\n",
    "                                transform=custom_transform)\n",
    "\n",
    "test_loader = DataLoader(dataset=test_dataset,\n",
    "                         batch_size=BATCH_SIZE,\n",
    "                         shuffle=False,\n",
    "                         num_workers=4) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 1 | Batch index: 0 | Batch size: 128\n",
      "Epoch: 2 | Batch index: 0 | Batch size: 128\n"
     ]
    }
   ],
   "source": [
    "device = torch.device(DEVICE if torch.cuda.is_available() else \"cpu\")\n",
    "torch.manual_seed(0)\n",
    "\n",
    "num_epochs = 2\n",
    "for epoch in range(num_epochs):\n",
    "\n",
    "    for batch_idx, (x, y) in enumerate(train_loader):\n",
    "        \n",
    "        print('Epoch:', epoch+1, end='')\n",
    "        print(' | Batch index:', batch_idx, end='')\n",
    "        print(' | Batch size:', y.size()[0])\n",
    "        \n",
    "        x = x.to(device)\n",
    "        y = y.to(device)\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following code cell that implements the ResNet-34 architecture is a derivative of the code provided at https://pytorch.org/docs/0.4.0/_modules/torchvision/models/resnet.html."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "##########################\n",
    "### MODEL\n",
    "##########################\n",
    "\n",
    "\n",
    "def conv3x3(in_planes, out_planes, stride=1):\n",
    "    \"\"\"3x3 convolution with padding\"\"\"\n",
    "    return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,\n",
    "                     padding=1, bias=False)\n",
    "\n",
    "\n",
    "class BasicBlock(nn.Module):\n",
    "    expansion = 1\n",
    "\n",
    "    def __init__(self, inplanes, planes, stride=1, downsample=None):\n",
    "        super(BasicBlock, self).__init__()\n",
    "        self.conv1 = conv3x3(inplanes, planes, stride)\n",
    "        self.bn1 = nn.BatchNorm2d(planes)\n",
    "        self.relu = nn.ReLU(inplace=True)\n",
    "        self.conv2 = conv3x3(planes, planes)\n",
    "        self.bn2 = nn.BatchNorm2d(planes)\n",
    "        self.downsample = downsample\n",
    "        self.stride = stride\n",
    "\n",
    "    def forward(self, x):\n",
    "        residual = x\n",
    "\n",
    "        out = self.conv1(x)\n",
    "        out = self.bn1(out)\n",
    "        out = self.relu(out)\n",
    "\n",
    "        out = self.conv2(out)\n",
    "        out = self.bn2(out)\n",
    "\n",
    "        if self.downsample is not None:\n",
    "            residual = self.downsample(x)\n",
    "\n",
    "        out += residual\n",
    "        out = self.relu(out)\n",
    "\n",
    "        return out\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "class ResNet(nn.Module):\n",
    "\n",
    "    def __init__(self, block, layers, num_classes, grayscale):\n",
    "        self.inplanes = 64\n",
    "        if grayscale:\n",
    "            in_dim = 1\n",
    "        else:\n",
    "            in_dim = 3\n",
    "        super(ResNet, self).__init__()\n",
    "        self.conv1 = nn.Conv2d(in_dim, 64, kernel_size=7, stride=2, padding=3,\n",
    "                               bias=False)\n",
    "        self.bn1 = nn.BatchNorm2d(64)\n",
    "        self.relu = nn.ReLU(inplace=True)\n",
    "        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)\n",
    "        self.layer1 = self._make_layer(block, 64, layers[0])\n",
    "        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)\n",
    "        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)\n",
    "        self.layer4 = self._make_layer(block, 512, layers[3], stride=2)\n",
    "        self.avgpool = nn.AvgPool2d(7, stride=1)\n",
    "        self.fc = nn.Linear(512 * block.expansion, num_classes)\n",
    "\n",
    "        for m in self.modules():\n",
    "            if isinstance(m, nn.Conv2d):\n",
    "                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels\n",
    "                m.weight.data.normal_(0, (2. / n)**.5)\n",
    "            elif isinstance(m, nn.BatchNorm2d):\n",
    "                m.weight.data.fill_(1)\n",
    "                m.bias.data.zero_()\n",
    "\n",
    "    def _make_layer(self, block, planes, blocks, stride=1):\n",
    "        downsample = None\n",
    "        if stride != 1 or self.inplanes != planes * block.expansion:\n",
    "            downsample = nn.Sequential(\n",
    "                nn.Conv2d(self.inplanes, planes * block.expansion,\n",
    "                          kernel_size=1, stride=stride, bias=False),\n",
    "                nn.BatchNorm2d(planes * block.expansion),\n",
    "            )\n",
    "\n",
    "        layers = []\n",
    "        layers.append(block(self.inplanes, planes, stride, downsample))\n",
    "        self.inplanes = planes * block.expansion\n",
    "        for i in range(1, blocks):\n",
    "            layers.append(block(self.inplanes, planes))\n",
    "\n",
    "        return nn.Sequential(*layers)\n",
    "\n",
    "    def forward(self, x):\n",
    "        x = self.conv1(x)\n",
    "        x = self.bn1(x)\n",
    "        x = self.relu(x)\n",
    "        x = self.maxpool(x)\n",
    "\n",
    "        x = self.layer1(x)\n",
    "        x = self.layer2(x)\n",
    "        x = self.layer3(x)\n",
    "        x = self.layer4(x)\n",
    "        # because MNIST is already 1x1 here:\n",
    "        # disable avg pooling\n",
    "        #x = self.avgpool(x)\n",
    "        \n",
    "        x = x.view(x.size(0), -1)\n",
    "        logits = self.fc(x)\n",
    "        probas = F.softmax(logits, dim=1)\n",
    "        return logits, probas\n",
    "\n",
    "\n",
    "\n",
    "def resnet34(num_classes):\n",
    "    \"\"\"Constructs a ResNet-34 model.\"\"\"\n",
    "    model = ResNet(block=BasicBlock, \n",
    "                   layers=[3, 4, 6, 3],\n",
    "                   num_classes=NUM_CLASSES,\n",
    "                   grayscale=GRAYSCALE)\n",
    "    return model\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "colab": {
     "autoexec": {
      "startup": false,
      "wait_interval": 0
     }
    },
    "colab_type": "code",
    "id": "_lza9t_uj5w1"
   },
   "outputs": [],
   "source": [
    "torch.manual_seed(RANDOM_SEED)\n",
    "model = resnet34(NUM_CLASSES)\n",
    "model.to(DEVICE)\n",
    "\n",
    "optimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE)  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "RAodboScj5w6"
   },
   "source": [
    "## Training"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "colab": {
     "autoexec": {
      "startup": false,
      "wait_interval": 0
     },
     "base_uri": "https://localhost:8080/",
     "height": 1547
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 2384585,
     "status": "ok",
     "timestamp": 1524976888520,
     "user": {
      "displayName": "Sebastian Raschka",
      "photoUrl": "//lh6.googleusercontent.com/-cxK6yOSQ6uE/AAAAAAAAAAI/AAAAAAAAIfw/P9ar_CHsKOQ/s50-c-k-no/photo.jpg",
      "userId": "118404394130788869227"
     },
     "user_tz": 240
    },
    "id": "Dzh3ROmRj5w7",
    "outputId": "5f8fd8c9-b076-403a-b0b7-fd2d498b48d7"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 001/010 | Batch 0000/8290 | Cost: 2.5425\n",
      "Epoch: 001/010 | Batch 0500/8290 | Cost: 0.3276\n",
      "Epoch: 001/010 | Batch 1000/8290 | Cost: 0.3386\n",
      "Epoch: 001/010 | Batch 1500/8290 | Cost: 0.3684\n",
      "Epoch: 001/010 | Batch 2000/8290 | Cost: 0.2080\n",
      "Epoch: 001/010 | Batch 2500/8290 | Cost: 0.3242\n",
      "Epoch: 001/010 | Batch 3000/8290 | Cost: 0.1639\n",
      "Epoch: 001/010 | Batch 3500/8290 | Cost: 0.1354\n",
      "Epoch: 001/010 | Batch 4000/8290 | Cost: 0.1846\n",
      "Epoch: 001/010 | Batch 4500/8290 | Cost: 0.3661\n",
      "Epoch: 001/010 | Batch 5000/8290 | Cost: 0.3528\n",
      "Epoch: 001/010 | Batch 5500/8290 | Cost: 0.1873\n",
      "Epoch: 001/010 | Batch 6000/8290 | Cost: 0.3726\n",
      "Epoch: 001/010 | Batch 6500/8290 | Cost: 0.2082\n",
      "Epoch: 001/010 | Batch 7000/8290 | Cost: 0.2256\n",
      "Epoch: 001/010 | Batch 7500/8290 | Cost: 0.1855\n",
      "Epoch: 001/010 | Batch 8000/8290 | Cost: 0.1332\n",
      "Epoch: 001/010 | Train: 93.190% | Validation: 92.841%\n",
      "Time elapsed: 8.59 min\n",
      "Epoch: 002/010 | Batch 0000/8290 | Cost: 0.2356\n",
      "Epoch: 002/010 | Batch 0500/8290 | Cost: 0.2356\n",
      "Epoch: 002/010 | Batch 1000/8290 | Cost: 0.2053\n",
      "Epoch: 002/010 | Batch 1500/8290 | Cost: 0.1255\n",
      "Epoch: 002/010 | Batch 2000/8290 | Cost: 0.1892\n",
      "Epoch: 002/010 | Batch 2500/8290 | Cost: 0.1587\n",
      "Epoch: 002/010 | Batch 3000/8290 | Cost: 0.1361\n",
      "Epoch: 002/010 | Batch 3500/8290 | Cost: 0.3038\n",
      "Epoch: 002/010 | Batch 4000/8290 | Cost: 0.3332\n",
      "Epoch: 002/010 | Batch 4500/8290 | Cost: 0.2460\n",
      "Epoch: 002/010 | Batch 5000/8290 | Cost: 0.2018\n",
      "Epoch: 002/010 | Batch 5500/8290 | Cost: 0.1409\n",
      "Epoch: 002/010 | Batch 6000/8290 | Cost: 0.3850\n",
      "Epoch: 002/010 | Batch 6500/8290 | Cost: 0.1791\n",
      "Epoch: 002/010 | Batch 7000/8290 | Cost: 0.2469\n",
      "Epoch: 002/010 | Batch 7500/8290 | Cost: 0.1429\n",
      "Epoch: 002/010 | Batch 8000/8290 | Cost: 0.2291\n",
      "Epoch: 002/010 | Train: 94.515% | Validation: 93.956%\n",
      "Time elapsed: 17.19 min\n",
      "Epoch: 003/010 | Batch 0000/8290 | Cost: 0.2039\n",
      "Epoch: 003/010 | Batch 0500/8290 | Cost: 0.1345\n",
      "Epoch: 003/010 | Batch 1000/8290 | Cost: 0.2768\n",
      "Epoch: 003/010 | Batch 1500/8290 | Cost: 0.1216\n",
      "Epoch: 003/010 | Batch 2000/8290 | Cost: 0.1107\n",
      "Epoch: 003/010 | Batch 2500/8290 | Cost: 0.1170\n",
      "Epoch: 003/010 | Batch 3000/8290 | Cost: 0.3591\n",
      "Epoch: 003/010 | Batch 3500/8290 | Cost: 0.1145\n",
      "Epoch: 003/010 | Batch 4000/8290 | Cost: 0.1540\n",
      "Epoch: 003/010 | Batch 4500/8290 | Cost: 0.1462\n",
      "Epoch: 003/010 | Batch 5000/8290 | Cost: 0.2056\n",
      "Epoch: 003/010 | Batch 5500/8290 | Cost: 0.1402\n",
      "Epoch: 003/010 | Batch 6000/8290 | Cost: 0.1879\n",
      "Epoch: 003/010 | Batch 6500/8290 | Cost: 0.1350\n",
      "Epoch: 003/010 | Batch 7000/8290 | Cost: 0.1906\n",
      "Epoch: 003/010 | Batch 7500/8290 | Cost: 0.1830\n",
      "Epoch: 003/010 | Batch 8000/8290 | Cost: 0.3022\n",
      "Epoch: 003/010 | Train: 95.162% | Validation: 94.417%\n",
      "Time elapsed: 25.74 min\n",
      "Epoch: 004/010 | Batch 0000/8290 | Cost: 0.2298\n",
      "Epoch: 004/010 | Batch 0500/8290 | Cost: 0.1867\n",
      "Epoch: 004/010 | Batch 1000/8290 | Cost: 0.2806\n",
      "Epoch: 004/010 | Batch 1500/8290 | Cost: 0.2951\n",
      "Epoch: 004/010 | Batch 2000/8290 | Cost: 0.1373\n",
      "Epoch: 004/010 | Batch 2500/8290 | Cost: 0.1479\n",
      "Epoch: 004/010 | Batch 3000/8290 | Cost: 0.1653\n",
      "Epoch: 004/010 | Batch 3500/8290 | Cost: 0.1761\n",
      "Epoch: 004/010 | Batch 4000/8290 | Cost: 0.1151\n",
      "Epoch: 004/010 | Batch 4500/8290 | Cost: 0.1239\n",
      "Epoch: 004/010 | Batch 5000/8290 | Cost: 0.1389\n",
      "Epoch: 004/010 | Batch 5500/8290 | Cost: 0.1788\n",
      "Epoch: 004/010 | Batch 6000/8290 | Cost: 0.1809\n",
      "Epoch: 004/010 | Batch 6500/8290 | Cost: 0.2698\n",
      "Epoch: 004/010 | Batch 7000/8290 | Cost: 0.2441\n",
      "Epoch: 004/010 | Batch 7500/8290 | Cost: 0.0742\n",
      "Epoch: 004/010 | Batch 8000/8290 | Cost: 0.1894\n",
      "Epoch: 004/010 | Train: 95.683% | Validation: 94.628%\n",
      "Time elapsed: 34.31 min\n",
      "Epoch: 005/010 | Batch 0000/8290 | Cost: 0.1017\n",
      "Epoch: 005/010 | Batch 0500/8290 | Cost: 0.0845\n",
      "Epoch: 005/010 | Batch 1000/8290 | Cost: 0.1353\n",
      "Epoch: 005/010 | Batch 1500/8290 | Cost: 0.1263\n",
      "Epoch: 005/010 | Batch 2000/8290 | Cost: 0.1884\n",
      "Epoch: 005/010 | Batch 2500/8290 | Cost: 0.1232\n",
      "Epoch: 005/010 | Batch 3000/8290 | Cost: 0.0987\n",
      "Epoch: 005/010 | Batch 3500/8290 | Cost: 0.1797\n",
      "Epoch: 005/010 | Batch 4000/8290 | Cost: 0.0795\n",
      "Epoch: 005/010 | Batch 4500/8290 | Cost: 0.0967\n",
      "Epoch: 005/010 | Batch 5000/8290 | Cost: 0.1592\n",
      "Epoch: 005/010 | Batch 5500/8290 | Cost: 0.1199\n",
      "Epoch: 005/010 | Batch 6000/8290 | Cost: 0.1348\n",
      "Epoch: 005/010 | Batch 6500/8290 | Cost: 0.0941\n",
      "Epoch: 005/010 | Batch 7000/8290 | Cost: 0.0563\n",
      "Epoch: 005/010 | Batch 7500/8290 | Cost: 0.1546\n",
      "Epoch: 005/010 | Batch 8000/8290 | Cost: 0.1088\n",
      "Epoch: 005/010 | Train: 96.155% | Validation: 94.870%\n",
      "Time elapsed: 42.90 min\n",
      "Epoch: 006/010 | Batch 0000/8290 | Cost: 0.0916\n",
      "Epoch: 006/010 | Batch 0500/8290 | Cost: 0.0671\n",
      "Epoch: 006/010 | Batch 1000/8290 | Cost: 0.1142\n",
      "Epoch: 006/010 | Batch 1500/8290 | Cost: 0.0678\n",
      "Epoch: 006/010 | Batch 2000/8290 | Cost: 0.1951\n",
      "Epoch: 006/010 | Batch 2500/8290 | Cost: 0.0357\n",
      "Epoch: 006/010 | Batch 3000/8290 | Cost: 0.0803\n",
      "Epoch: 006/010 | Batch 3500/8290 | Cost: 0.2199\n",
      "Epoch: 006/010 | Batch 4000/8290 | Cost: 0.1880\n",
      "Epoch: 006/010 | Batch 4500/8290 | Cost: 0.2105\n",
      "Epoch: 006/010 | Batch 5000/8290 | Cost: 0.1048\n",
      "Epoch: 006/010 | Batch 5500/8290 | Cost: 0.2252\n",
      "Epoch: 006/010 | Batch 6000/8290 | Cost: 0.1849\n",
      "Epoch: 006/010 | Batch 6500/8290 | Cost: 0.1363\n",
      "Epoch: 006/010 | Batch 7000/8290 | Cost: 0.1704\n",
      "Epoch: 006/010 | Batch 7500/8290 | Cost: 0.1552\n",
      "Epoch: 006/010 | Batch 8000/8290 | Cost: 0.1243\n",
      "Epoch: 006/010 | Train: 96.465% | Validation: 94.885%\n",
      "Time elapsed: 51.39 min\n",
      "Epoch: 007/010 | Batch 0000/8290 | Cost: 0.1435\n",
      "Epoch: 007/010 | Batch 0500/8290 | Cost: 0.0844\n",
      "Epoch: 007/010 | Batch 1000/8290 | Cost: 0.1750\n",
      "Epoch: 007/010 | Batch 1500/8290 | Cost: 0.1444\n",
      "Epoch: 007/010 | Batch 2000/8290 | Cost: 0.1030\n",
      "Epoch: 007/010 | Batch 2500/8290 | Cost: 0.0450\n",
      "Epoch: 007/010 | Batch 3000/8290 | Cost: 0.1276\n",
      "Epoch: 007/010 | Batch 3500/8290 | Cost: 0.1663\n",
      "Epoch: 007/010 | Batch 4000/8290 | Cost: 0.1815\n",
      "Epoch: 007/010 | Batch 4500/8290 | Cost: 0.0618\n",
      "Epoch: 007/010 | Batch 5000/8290 | Cost: 0.2072\n",
      "Epoch: 007/010 | Batch 5500/8290 | Cost: 0.1557\n",
      "Epoch: 007/010 | Batch 6000/8290 | Cost: 0.1042\n",
      "Epoch: 007/010 | Batch 6500/8290 | Cost: 0.0807\n",
      "Epoch: 007/010 | Batch 7000/8290 | Cost: 0.2010\n",
      "Epoch: 007/010 | Batch 7500/8290 | Cost: 0.1398\n",
      "Epoch: 007/010 | Batch 8000/8290 | Cost: 0.1735\n",
      "Epoch: 007/010 | Train: 96.858% | Validation: 94.951%\n",
      "Time elapsed: 60.00 min\n",
      "Epoch: 008/010 | Batch 0000/8290 | Cost: 0.0207\n",
      "Epoch: 008/010 | Batch 0500/8290 | Cost: 0.1454\n",
      "Epoch: 008/010 | Batch 1000/8290 | Cost: 0.1000\n",
      "Epoch: 008/010 | Batch 1500/8290 | Cost: 0.1481\n",
      "Epoch: 008/010 | Batch 2000/8290 | Cost: 0.0647\n",
      "Epoch: 008/010 | Batch 2500/8290 | Cost: 0.1113\n",
      "Epoch: 008/010 | Batch 3000/8290 | Cost: 0.0874\n",
      "Epoch: 008/010 | Batch 3500/8290 | Cost: 0.1194\n",
      "Epoch: 008/010 | Batch 4000/8290 | Cost: 0.2510\n",
      "Epoch: 008/010 | Batch 4500/8290 | Cost: 0.1316\n",
      "Epoch: 008/010 | Batch 5000/8290 | Cost: 0.0617\n",
      "Epoch: 008/010 | Batch 5500/8290 | Cost: 0.1925\n",
      "Epoch: 008/010 | Batch 6000/8290 | Cost: 0.0551\n",
      "Epoch: 008/010 | Batch 6500/8290 | Cost: 0.0698\n",
      "Epoch: 008/010 | Batch 7000/8290 | Cost: 0.0733\n",
      "Epoch: 008/010 | Batch 7500/8290 | Cost: 0.0917\n",
      "Epoch: 008/010 | Batch 8000/8290 | Cost: 0.0579\n",
      "Epoch: 008/010 | Train: 97.120% | Validation: 94.812%\n",
      "Time elapsed: 68.50 min\n",
      "Epoch: 009/010 | Batch 0000/8290 | Cost: 0.0515\n",
      "Epoch: 009/010 | Batch 0500/8290 | Cost: 0.2004\n",
      "Epoch: 009/010 | Batch 1000/8290 | Cost: 0.0326\n",
      "Epoch: 009/010 | Batch 1500/8290 | Cost: 0.2234\n",
      "Epoch: 009/010 | Batch 2000/8290 | Cost: 0.0449\n",
      "Epoch: 009/010 | Batch 2500/8290 | Cost: 0.1230\n",
      "Epoch: 009/010 | Batch 3000/8290 | Cost: 0.1141\n",
      "Epoch: 009/010 | Batch 3500/8290 | Cost: 0.1092\n",
      "Epoch: 009/010 | Batch 4000/8290 | Cost: 0.0691\n",
      "Epoch: 009/010 | Batch 4500/8290 | Cost: 0.0492\n",
      "Epoch: 009/010 | Batch 5000/8290 | Cost: 0.1390\n",
      "Epoch: 009/010 | Batch 5500/8290 | Cost: 0.0819\n",
      "Epoch: 009/010 | Batch 6000/8290 | Cost: 0.0978\n",
      "Epoch: 009/010 | Batch 6500/8290 | Cost: 0.0914\n",
      "Epoch: 009/010 | Batch 7000/8290 | Cost: 0.1712\n",
      "Epoch: 009/010 | Batch 7500/8290 | Cost: 0.0403\n",
      "Epoch: 009/010 | Batch 8000/8290 | Cost: 0.1668\n",
      "Epoch: 009/010 | Train: 97.539% | Validation: 94.882%\n",
      "Time elapsed: 77.13 min\n",
      "Epoch: 010/010 | Batch 0000/8290 | Cost: 0.0739\n",
      "Epoch: 010/010 | Batch 0500/8290 | Cost: 0.1147\n",
      "Epoch: 010/010 | Batch 1000/8290 | Cost: 0.0982\n",
      "Epoch: 010/010 | Batch 1500/8290 | Cost: 0.0961\n",
      "Epoch: 010/010 | Batch 2000/8290 | Cost: 0.0778\n",
      "Epoch: 010/010 | Batch 2500/8290 | Cost: 0.0967\n",
      "Epoch: 010/010 | Batch 3000/8290 | Cost: 0.1380\n",
      "Epoch: 010/010 | Batch 3500/8290 | Cost: 0.0326\n",
      "Epoch: 010/010 | Batch 4000/8290 | Cost: 0.0566\n",
      "Epoch: 010/010 | Batch 4500/8290 | Cost: 0.2215\n",
      "Epoch: 010/010 | Batch 5000/8290 | Cost: 0.0623\n",
      "Epoch: 010/010 | Batch 5500/8290 | Cost: 0.1029\n",
      "Epoch: 010/010 | Batch 6000/8290 | Cost: 0.0739\n",
      "Epoch: 010/010 | Batch 6500/8290 | Cost: 0.1266\n",
      "Epoch: 010/010 | Batch 7000/8290 | Cost: 0.0917\n",
      "Epoch: 010/010 | Batch 7500/8290 | Cost: 0.0626\n",
      "Epoch: 010/010 | Batch 8000/8290 | Cost: 0.0368\n",
      "Epoch: 010/010 | Train: 97.730% | Validation: 94.717%\n",
      "Time elapsed: 85.68 min\n",
      "Total Training Time: 85.68 min\n"
     ]
    }
   ],
   "source": [
    "def compute_accuracy(model, data_loader, device):\n",
    "    correct_pred, num_examples = 0, 0\n",
    "    for i, (features, targets) in enumerate(data_loader):\n",
    "            \n",
    "        features = features.to(device)\n",
    "        targets = targets.to(device)\n",
    "\n",
    "        logits, probas = model(features)\n",
    "        _, predicted_labels = torch.max(probas, 1)\n",
    "        num_examples += targets.size(0)\n",
    "        correct_pred += (predicted_labels == targets).sum()\n",
    "    return correct_pred.float()/num_examples * 100\n",
    "    \n",
    "\n",
    "start_time = time.time()\n",
    "for epoch in range(NUM_EPOCHS):\n",
    "    \n",
    "    model.train()\n",
    "    for batch_idx, (features, targets) in enumerate(train_loader):\n",
    "        \n",
    "        features = features.to(DEVICE)\n",
    "        targets = targets.to(DEVICE)\n",
    "            \n",
    "        ### FORWARD AND BACK PROP\n",
    "        logits, probas = model(features)\n",
    "        cost = F.cross_entropy(logits, targets)\n",
    "        optimizer.zero_grad()\n",
    "        \n",
    "        cost.backward()\n",
    "        \n",
    "        ### UPDATE MODEL PARAMETERS\n",
    "        optimizer.step()\n",
    "        \n",
    "        ### LOGGING\n",
    "        if not batch_idx % 500:\n",
    "            print ('Epoch: %03d/%03d | Batch %04d/%04d | Cost: %.4f' \n",
    "                   %(epoch+1, NUM_EPOCHS, batch_idx, \n",
    "                     len(train_loader), cost))\n",
    "\n",
    "        \n",
    "\n",
    "    model.eval()\n",
    "    with torch.set_grad_enabled(False): # save memory during inference\n",
    "        print('Epoch: %03d/%03d | Train: %.3f%% | Validation: %.3f%%' % (\n",
    "              epoch+1, NUM_EPOCHS, \n",
    "              compute_accuracy(model, train_loader, device=DEVICE),\n",
    "              compute_accuracy(model, valid_loader, device=DEVICE)))\n",
    "        \n",
    "    print('Time elapsed: %.2f min' % ((time.time() - start_time)/60))\n",
    "    \n",
    "print('Total Training Time: %.2f min' % ((time.time() - start_time)/60))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "paaeEQHQj5xC"
   },
   "source": [
    "## Evaluation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "colab": {
     "autoexec": {
      "startup": false,
      "wait_interval": 0
     },
     "base_uri": "https://localhost:8080/",
     "height": 34
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 6514,
     "status": "ok",
     "timestamp": 1524976895054,
     "user": {
      "displayName": "Sebastian Raschka",
      "photoUrl": "//lh6.googleusercontent.com/-cxK6yOSQ6uE/AAAAAAAAAAI/AAAAAAAAIfw/P9ar_CHsKOQ/s50-c-k-no/photo.jpg",
      "userId": "118404394130788869227"
     },
     "user_tz": 240
    },
    "id": "gzQMWKq5j5xE",
    "outputId": "de7dc005-5eeb-4177-9f9f-d9b5d1358db9"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test accuracy: 97.73%\n"
     ]
    }
   ],
   "source": [
    "with torch.set_grad_enabled(False): # save memory during inference\n",
    "    print('Test accuracy: %.2f%%' % (compute_accuracy(model, test_loader, device=DEVICE)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAQIklEQVR4nO3df4xV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P4PaZq41BtvbUoAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "for batch_idx, (features, targets) in enumerate(test_loader):\n",
    "\n",
    "    features = features\n",
    "    targets = targets\n",
    "    break\n",
    "    \n",
    "    \n",
    "nhwc_img = np.transpose(features[5], axes=(1, 2, 0))\n",
    "nhw_img = np.squeeze(nhwc_img.numpy(), axis=2)\n",
    "plt.imshow(nhw_img, cmap='Greys');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Probability Washing Machine 100.00%\n"
     ]
    }
   ],
   "source": [
    "model.eval()\n",
    "logits, probas = model(features.to(device)[0, None])\n",
    "print('Probability Washing Machine %.2f%%' % (probas[0][4]*100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PIL.Image   6.2.0\n",
      "pandas      0.24.2\n",
      "torch       1.3.0\n",
      "numpy       1.17.2\n",
      "torchvision 0.4.1a0+d94043a\n",
      "matplotlib  3.1.0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%watermark -iv"
   ]
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "collapsed_sections": [],
   "default_view": {},
   "name": "convnet-vgg16.ipynb",
   "provenance": [],
   "version": "0.3.2",
   "views": {}
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "371px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
